package com.starlink.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.starlink.common.domain.BaseEntity;
import com.starlink.model.customdo.Question;
import com.starlink.model.domain.MallBooksDO;
import com.starlink.model.domain.SchoolbagOrderReadRecordDO;
import com.starlink.model.dtomapper.MallBooksDtoMapper;
import com.starlink.model.util.MallBooksConvert;
import com.starlink.service.MallBooksService;
import com.starlink.service.SchoolbagOrderReadRecordService;
import com.starlink.utils.SchoolBagTestingUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.SchoolbagReadTestingDtoMapper;
import com.starlink.model.domain.SchoolbagReadTestingDO;
import com.starlink.mapper.SchoolbagReadTestingMapper;
import com.starlink.model.request.SchoolbagReadTestingRequest;
import com.starlink.service.SchoolbagReadTestingService;
import com.starlink.model.util.SchoolbagReadTestingConvert;
import com.starlink.model.vo.SchoolbagReadTestingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class SchoolbagReadTestingServiceImpl extends ServiceImpl<SchoolbagReadTestingMapper, SchoolbagReadTestingDO> implements SchoolbagReadTestingService {
    @Autowired
    private SchoolbagReadTestingMapper schoolbagReadTestingMapper;
    @Autowired
    private MallBooksService mallBooksService;
    @Autowired
    private SchoolbagOrderReadRecordService schoolbagOrderReadRecordService;

    @Override
    public SchoolbagReadTestingVO getByKey(long id) {
        SchoolbagReadTestingVO schoolbagReadTestingVO = SchoolbagReadTestingDtoMapper.INSTANCE.do2VO(getById(id));
        return schoolbagReadTestingVO;
    }

    @Override
    public PageInfo<SchoolbagReadTestingVO> pageQuery(Pageable pageable,SchoolbagReadTestingRequest request) {
        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<SchoolbagReadTestingDO> list = schoolbagReadTestingMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<SchoolbagReadTestingVO> schoolbagReadTestingVOList = SchoolbagReadTestingConvert.buildSchoolbagReadTestingVoList(list);
        return new PageInfo(schoolbagReadTestingVOList);
    }

    @Override
    public List<SchoolbagReadTestingVO> listQuery(SchoolbagReadTestingRequest request) {
        List<SchoolbagReadTestingDO> list = schoolbagReadTestingMapper.selectDOList(SchoolbagReadTestingDtoMapper.INSTANCE.request2DO(request));
        List<SchoolbagReadTestingVO> schoolbagReadTestingVOS = SchoolbagReadTestingConvert.buildSchoolbagReadTestingVoList(list);
        return schoolbagReadTestingVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SchoolbagReadTestingRequest request) {
        SchoolbagReadTestingDO entity = new SchoolbagReadTestingDO();//SchoolbagReadTestingDtoMapper.INSTANCE.request2DO(request);
        entity.setOrderId(request.getOrderId());
        entity.setOrderReadId(request.getOrderReadId());
        entity.setBookId(request.getBookId());
        entity.setTestingRecord(JSON.toJSONString(request.getTestingRecord()));
        return save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(SchoolbagReadTestingRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(SchoolbagReadTestingDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        schoolbagReadTestingMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public List<SchoolbagReadTestingVO> getBySchoolbagOrderId(SchoolbagReadTestingRequest request) {
        SchoolbagOrderReadRecordDO schoolbagOrderReadRecordDo = schoolbagOrderReadRecordService.lambdaQuery().eq(BaseEntity::getId, request.getOrderReadId()).one();
        AssertUtil.assertNotNull(schoolbagOrderReadRecordDo, "分组阅读记录不存在");
        // 查询分期是否生成过阅读记录
        List<SchoolbagReadTestingDO> list = schoolbagReadTestingMapper.selectDOList(SchoolbagReadTestingDtoMapper.INSTANCE.request2DO(request));
        if (CollectionUtils.isEmpty(list)) {
            List<MallBooksDO> bookList = mallBooksService.queryByGroupBagId(schoolbagOrderReadRecordDo.getSchoolbagGroupBagId());
            List<SchoolbagReadTestingVO> result = Lists.newArrayList();
            for (MallBooksDO mallBooksDO : bookList) {
                SchoolbagReadTestingVO testingVO = new SchoolbagReadTestingVO();
                testingVO.setOrderId(schoolbagOrderReadRecordDo.getOrderId());
                testingVO.setOrderReadId(schoolbagOrderReadRecordDo.getId());
                testingVO.setBookId(mallBooksDO.getId());
                testingVO.setBookInfo(MallBooksDtoMapper.INSTANCE.do2VO(mallBooksDO));
                testingVO.setTestingRecord(SchoolBagTestingUtil.getInitQuestionList());
                result.add(testingVO);
            }
            return result;
        } else {
            List<SchoolbagReadTestingVO> schoolbagReadTestingVOS = SchoolbagReadTestingConvert.buildSchoolbagReadTestingVoList(list);
            for (SchoolbagReadTestingVO schoolbagReadTestingVO : schoolbagReadTestingVOS) {
                // 查询书籍信息
                MallBooksDO mallBooksDO  = mallBooksService.lambdaQuery().eq(BaseEntity::getId, schoolbagReadTestingVO.getBookId()).one();
                schoolbagReadTestingVO.setBookInfo(MallBooksDtoMapper.INSTANCE.do2VO(mallBooksDO));
            }
            return schoolbagReadTestingVOS;
        }
    }

    @Transactional
    @Override
    public Boolean batchAdd(List<SchoolbagReadTestingRequest> batchRequest) {
        for (SchoolbagReadTestingRequest schoolbagReadTestingRequest : batchRequest) {
            add(schoolbagReadTestingRequest);
        }
        return true;
    }
}
